home *** CD-ROM | disk | FTP | other *** search
- /* tascii - displays an ASCII character table on standard output.
- Copyright (C) 1996, Robert A. Biersack
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
-
-
- #include <stdio.h>
-
-
- /* Translation tables. */
- char *decimal_number[128] =
- {"000", "001", "002", "003", "004", "005", "006", "007", "008", "009",
- "010", "011", "012", "013", "014", "015", "016", "017", "018", "019",
- "020", "021", "022", "023", "024", "025", "026", "027", "028", "029",
- "030", "031", "032", "033", "034", "035", "036", "037", "038", "039",
- "040", "041", "042", "043", "044", "045", "046", "047", "048", "049",
- "050", "051", "052", "053", "054", "055", "056", "057", "058", "059",
- "060", "061", "062", "063", "064", "065", "066", "067", "068", "069",
- "070", "071", "072", "073", "074", "075", "076", "077", "078", "079",
- "080", "081", "082", "083", "084", "085", "086", "087", "088", "089",
- "090", "091", "092", "093", "094", "095", "096", "097", "098", "099",
- "100", "101", "102", "103", "104", "105", "106", "107", "108", "109",
- "110", "111", "112", "113", "114", "115", "116", "117", "118", "119",
- "120", "121", "122", "123", "124", "125", "126", "127"};
-
- char *octal_number[128] =
- {"000", "001", "002", "003", "004", "005", "006", "007",
- "010", "011", "012", "013", "014", "015", "016", "017",
- "020", "021", "022", "023", "024", "025", "026", "027",
- "030", "031", "032", "033", "034", "035", "036", "037",
- "040", "041", "042", "043", "044", "045", "046", "047",
- "050", "051", "052", "053", "054", "055", "056", "057",
- "060", "061", "062", "063", "064", "065", "066", "067",
- "070", "071", "072", "073", "074", "075", "076", "077",
- "100", "101", "102", "103", "104", "105", "106", "107",
- "110", "111", "112", "113", "114", "115", "116", "117",
- "120", "121", "122", "123", "124", "125", "126", "127",
- "130", "131", "132", "133", "134", "135", "136", "137",
- "140", "141", "142", "143", "144", "145", "146", "147",
- "150", "151", "152", "153", "154", "155", "156", "157",
- "160", "161", "162", "163", "164", "165", "166", "167",
- "170", "171", "172", "173", "174", "175", "176", "177"};
-
- char *hex_number[128] =
- {"00","01","02","03","04","05","06","07","08","09","0A","0B","0C","0D","0E","0F",
- "10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F",
- "20","21","22","23","24","25","26","27","28","29","2A","2B","2C","2D","2E","2F",
- "30","31","32","33","34","35","36","37","38","39","3A","3B","3C","3D","3E","3F",
- "40","41","42","43","44","45","46","47","48","49","4A","4B","4C","4D","4E","4F",
- "50","51","52","53","54","55","56","57","58","59","5A","5B","5C","5D","5E","5F",
- "60","61","62","63","64","65","66","67","68","69","6A","6B","6C","6D","6E","6F",
- "70","71","72","73","74","75","76","77","78","79","7A","7B","7C","7D","7E","7F"
- };
-
- char *ascii_char[128] =
- {"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
- "BS ", "HT ", "LF ", "VT ", "FF ", "CR ", "SO ", "SI ",
- "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
- "CAN", "EM ", "SUB", "ESC", "FS ", "GS ", "RS ", "US ",
- "SP ", "! ", "\" ", "# ", "$ ", "% ", "& ", "' ",
- "( ", ") ", "* ", "+ ", ", ", "- ", ". ", "/ ",
- "0 ", "1 ", "2 ", "3 ", "4 ", "5 ", "6 ", "7 ",
- "8 ", "9 ", ": ", "; ", "< ", "= ", "> ", "? ",
- "@ ", "A ", "B ", "C ", "D ", "E ", "F ", "G ",
- "H ", "I ", "J ", "K ", "L ", "M ", "N ", "O ",
- "P ", "Q ", "R ", "S ", "T ", "U ", "V ", "W ",
- "X ", "Y ", "Z ", "[ ", "\\ ", "] ", "^ ", "_ ",
- "` ", "a ", "b ", "c ", "d ", "e ", "f ", "g ",
- "h ", "i ", "j ", "k ", "l ", "m ", "n ", "o ",
- "p ", "q ", "r ", "s ", "t ", "u ", "v ", "w ",
- "x ", "y ", "z ", "{ ", "| ", "} ", "~ ", "DEL"};
-
- /* Name under which this program was invoked. */
- char *program_name;
-
- /* Name and version number of program. */
- const char *version_string = "tascii v1.0";
-
- char long_option_version[] = "--version";
- char long_option_help[] = "--help";
-
- void print_table (char *p[]);
- int compare (char *s, char *t);
- void usage (int status);
-
- int
- main (int argc, char **argv)
- {
- /* Use flags to keep track of options found. */
- int dflag = 0;
- int oflag = 0;
- int xflag = 0;
- int hflag = 0;
- int vflag = 0;
-
- int i;
- int count;
-
- char *nextchar;
-
- /* Save the program name for future use. */
- program_name = argv[0];
-
- /* Check for --version or --help. */
- for (count = 1; count < argc; count++)
- {
- if (compare(long_option_version, argv[count]))
- vflag = 1;
- if (compare(long_option_help, argv[count]))
- hflag = 1;
- }
-
- /* If --version or --help, print message and quit. */
- if (vflag)
- {
- printf("%s\n", version_string);
- return 0;
- }
-
- if (hflag)
- {
- usage (0);
- return 0;
- }
-
- /* Check for -d -o -x options. */
- for (count = 1; count < argc; count++)
- {
- nextchar = argv[count];
- if (nextchar[0] != '-')
- {
- /* Missing '-' before the option argument. */
- fprintf(stderr, "%s: illegal non-option argument -- %s\n",
- program_name, argv[count]);
- usage(1);
- return 1;
- }
-
- if (nextchar[1] == '\0')
- {
- /* Found '-' without an option argument. */
- fprintf(stderr, "%s: illegal missing option -- %s\n",
- program_name, argv[count]);
- usage(1);
- return 1;
- }
-
- if (nextchar[1] == '-')
- {
- /* Found '--'. */
- break;
- }
-
- for (i = 1; nextchar[i] != '\0'; i++)
- {
- switch (nextchar[i])
- {
- case 'd':{
- dflag = 1;
- break;}
- case 'o':{
- oflag = 1;
- break;}
- case 'x':{
- xflag = 1;
- break;}
- default:{
- /* Found a non-option. */
- fprintf(stderr, "%s: illegal option -- %c\n",
- program_name, nextchar[i]);
- usage(1);
- return 1;}
- }
- }
- }
-
- if (!(oflag || xflag))
- /* Unconditionally set dflag if no other option was selected. */
- dflag = 1;
-
- if (dflag)
- print_table(decimal_number);
-
- if (oflag)
- print_table(octal_number);
-
- if (xflag)
- print_table(hex_number);
-
- return 0;
- }
-
-
- /* Print an ASCII table (16 rows, 8 cols) to standard output. */
- void
- print_table (char *p[])
- {
- int ctr;
- int col0, col1, col2, col3, col4, col5, col6, col7;
-
- printf("\n%41s\n\n","ASCII Character Table");
-
- for (ctr = 0; ctr < 16; ctr++)
- {
- col0 = ctr;
- col1 = ctr + 16;
- col2 = ctr + 32;
- col3 = ctr + 48;
- col4 = ctr + 64;
- col5 = ctr + 80;
- col6 = ctr + 96;
- col7 = ctr + 112;
-
- printf("%3s %3s %4s %3s %4s %3s %3s %3s%3s %3s%3s %3s%3s %3s%3s %3s\n",
- p[col0], ascii_char[col0], p[col1], ascii_char[col1],
- p[col2], ascii_char[col2], p[col3], ascii_char[col3],
- p[col4], ascii_char[col4], p[col5], ascii_char[col5],
- p[col6], ascii_char[col6], p[col7], ascii_char[col7]);
- }
- printf("\n");
- }
-
- /* Compare two strings and return 1 if they are equal. */
- int
- compare (char *s, char *t)
- {
- int i;
-
- for (i = 0; s[i] == t[i]; i++)
- if (s[i] == '\0')
- return 1;
- /* If strings are not equal then return 0 */
- return 0;
- }
-
- /* Print some friendly help information. */
- void
- usage (int status)
- {
- if (status != 0)
- fprintf (stderr, "Try '%s --help' for more information.\n",
- program_name);
- else
- {
- printf ("\
- Usage: %s [OPTION]\n\
- ",
- program_name);
- printf ("\
- \n\
- -d decimal notation format\n\
- -o octal notation format\n\
- -x hexadecimal notation format\n\
- \n\
- --help display this help and exit\n\
- --version output version information and exit\n\
- \n\
- With no options, the default is an ASCII character table in\n\
- decimal notation format.\n\
- ");
- }
- }
-
-